package com.ark.cloud.dataservices.modules.sys.entity.vo;

import com.ark.cloud.dataservices.modules.sys.entity.po.Role;
import com.ark.cloud.dataservices.modules.sys.entity.po.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright(c): 2021 Cloud-Ark Inc.
 * Author(s): Ma Wei (mawei@cloud-ark.com)
 */
public class CustomerUserDetails implements UserDetails {
    private User user;

    private List<Role> permissionList;

    public CustomerUserDetails(User user,List<Role> permissionList) {
        this.user = user;
        this.permissionList = permissionList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorityList = this.permissionList.stream()
                //返回不等于null的权限值
                .filter(umsPermission -> umsPermission.getCode() != null)
                //将UmsPermission类型集合转换成SimpleGrantedAuthority类型集合
                .map(umsPermission -> new SimpleGrantedAuthority(umsPermission.getCode()))
                .collect(Collectors.toList());
        return authorityList;
    }

    @Override
    public String getPassword() {
        return this.user.getPassword();
    }

    @Override
    public String getUsername() {
        return this.user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.user.getAccountNonExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.user.getAccountNonLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.user.getCredentialsNonExpired();
    }
    /**
     * 账号是否启用
     *
     * @return boolean
     */
    @Override
    public boolean isEnabled() {
        return this.user.getEnabled();
    }
}
